package cn.edu.hunau.dao.impl;

import cn.edu.hunau.dao.UserDao;
import cn.edu.hunau.exception.DaoException;
import cn.edu.hunau.pojo.User;
import cn.edu.hunau.utils.DBUtils;

/**
 * @Author 苏佳
 * @Date 2022 12 02 11 16
 **/
public class UserDaoImpl implements UserDao {
    @Override
    public User createUser(User user) throws DaoException {
        String sql="insert into t_user(username,pwd) values(?,md5(?))";

        int id = DBUtils.updateReturnAutoincrementPk(sql, new String[]{"id"}, user.getUsername(), user.getPwd());
        user.setId(id);
        return user;
    }

    @Override
    public User queryByUserNameAndPwd(String username, String pwd) throws DaoException {
        String sql="select id,username from t_user where username=? and pwd=md5(?)";

        return DBUtils.query(sql,(rs)->{
            User u=null;
            if(rs.next()){
                u=new User();
                u.setId(rs.getInt("id"));
                u.setUsername(rs.getString("username"));
            }
            return u;
        },username,pwd);
    }

    @Override
    public User queryByTelAndPwd(String tel, String pwd) throws DaoException {
        String sql="select id,username from t_user where tel=? and pwd=md5(?)";

        return DBUtils.query(sql,(rs)->{
            User u=null;
            if(rs.next()){
                u=new User();
                u.setId(rs.getInt("id"));
                u.setUsername(rs.getString("username"));
            }
            return u;
        },tel,pwd);
    }

    @Override
    public User queryByUsername(String username) throws DaoException {
        String sql="select id,username from t_user where username=?";
        return DBUtils.query(sql,rs -> {
            User u=null;
            if(rs.next()){
                u=new User();
                u.setId(rs.getInt("id"));
                u.setUsername(rs.getString("username"));
            }
            return u;
        },username);
    }

    @Override
    public void bingingTel(String tel,Integer uid) throws DaoException {
        String sql="update t_user set tel=? where id=?";
        DBUtils.update(sql,tel,uid);
    }

    @Override
    public void updatePwd(String newPwd, Integer uid) throws DaoException {
        String sql="update t_user set pwd=md5(?) where id=?";
        DBUtils.update(sql,newPwd,uid);
    }

    @Override
    public String queryPwdByUid(Integer uid) throws DaoException {
        String sql="select pwd from t_user where id=?";
        return DBUtils.query(sql,rs -> {
            String oldPwd="";
            if(rs.next()){
                oldPwd=rs.getString("pwd");
            }
            return oldPwd;
        },uid);
    }

    @Override
    public User queryInforByUid(Integer uid) throws DaoException {
        String sql="select id,username,tel from t_user where id=?";

        return DBUtils.query(sql,rs -> {
            User u=null;
            if(rs.next()){
                u=new User();
                u.setId(rs.getInt("id"));
                u.setUsername(rs.getString("username"));
                u.setTel(rs.getString("tel"));
            }
            return u;
        },uid);
    }
}
